package com.ssm.service.impl;

import com.ssm.entity.Dept;
import com.ssm.entity.PageResult;
import com.ssm.entity.QueryObject;
import com.ssm.mapper.DeptMapper;
import com.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    @Transactional
    public void save(Dept dept) {
        if (dept.getName() == null) {
            throw new RuntimeException("部门名称不能为空");
        }
        deptMapper.insert(dept);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        deptMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Dept dept) {
        deptMapper.updateByPrimaryKey(dept);
    }

    @Override
    public Dept sqlById(Long id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Dept> selAll() {
        return deptMapper.selectAll();
    }

    @Override
    @Transactional
    public PageResult<Dept> query(QueryObject queryObject) {
        //查询总条数
        int i = deptMapper.selForCount(queryObject);
        if (i == 0) {
            return new PageResult<Dept>(queryObject.getCurrentPage(), queryObject.getPageSize(), i, Collections.emptyList());
        }
        List<Dept> deptList = deptMapper.selForList(queryObject);
        return new PageResult<Dept>(queryObject.getCurrentPage(), queryObject.getPageSize(), i, deptList);
    }

}
